<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\model\QqMaterialPicDailyReportExtModel;
use app\model\QqMaterialPicDailyReportHistoryExtModel;
use app\model\QqMaterialPicDailyReportHistoryModel;
use app\model\QqMaterialPicDailyReportModel;
use think\console\Input;
use think\console\Output;
use think\Facade\Db;

class QqMaterialPicDailyReportHistoryWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('qq_material_pic_daily_stat_history_work');
        $this->setDescription('广点通-图片素材历史数据写入');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======广点通-素材历史日流水历史开始' . date('Y-m-d H:i:s', time()) . '=======');
        $result = $this->_execQqMaterailDailyReportHistory();
        $output->writeln('=======广点通-素材历史日流水历史结束' . date('Y-m-d H:i:s', time()) . '=======');
        $output->writeln('success');
    }

    /**
     * 执行历史任务
     */
    private function _execQqMaterailDailyReportHistory(): bool
    {
        $statDate = intval(date('Ymd', strtotime('-90 day')));
        $list = $this->_getQqMaterailDailyReportList($statDate);
        $extList = $this->_getQqMaterailDailyReportExtList($statDate);

        if (! $list || ! $extList) {
            return false;
        }

        if (count($list) != count($extList)) {
            return false;
        }

        Db::startTrans();
        $one = $this->_insertQqMaterailDailyReportHistoryData($list, $extList);
        $two = $this->_deleteQqMaterailDailyReportData($statDate);

        if ($one && $two) {
            Db::commit();
            return true;
        }

        Db::rollback();
        return false;
    }

    /**
     * 批量插入历史数据.
     * @param array $allData 全部数据
     */
    private function _insertQqMaterailDailyReportHistoryData(array $allData = [], array $allExtData = []): bool
    {
        try {
            $one = (new QqMaterialPicDailyReportHistoryModel())->insertAll($allData);
            $two = (new QqMaterialPicDailyReportHistoryExtModel())->insertAll($allExtData);

            if ($one && $two) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
        }
    }

    /**
     * 删除记录.
     * @param int $statDate 日期
     */
    private function _deleteQqMaterailDailyReportData(int $statDate = 0): bool
    {
        try {
            $where = [
                ['stat_date', '<', $statDate],
            ];

            $one = (new QqMaterialPicDailyReportModel())->where($where)->delete();
            $two = (new QqMaterialPicDailyReportExtModel())->where($where)->delete();

            if ($one && $two) {
                return true;
            }

            return false;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
        }
    }

    /**
     * 获取所有记录.
     * @param int $statDate 日期
     * @throws
     */
    private function _getQqMaterailDailyReportList(int $statDate = 0): array
    {
        $where = [
            ['stat_date', '<', $statDate],
        ];

        return (new QqMaterialPicDailyReportModel())->where($where)->withoutField('id')->select()->toArray();
    }

    /**
     * 获取所有素材扩展.
     * @param int $statDate 日期
     * @throws
     */
    private function _getQqMaterailDailyReportExtList(int $statDate = 0): array
    {
        $where = [
            ['stat_date', '<', $statDate],
        ];

        return (new QqMaterialPicDailyReportExtModel())->where($where)->withoutField('id')->select()->toArray();
    }
}
